<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="甲方" prop="thirdName">
        <el-input v-model="queryParams.thirdName" placeholder="请输入甲方" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="项目经理" prop="projectLeaderUid">
        <user-select ref="UserSelect" serselect :pModel.sync="queryParams.projectLeaderUid" placeholder="请选择项目经理" roleId="11" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目主管" prop="directorUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.directorUid" placeholder="请选择项目主管" roleId="12" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="区域经理" prop="areaManagerUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.areaManagerUid" placeholder="请选择区域经理" roleId="10" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目类型" prop="isNormal">
        <el-select v-model="queryParams.isNormal" placeholder="请选择项目类型" clearable size="small" @change="handleQuery">
          <el-option v-for="dict in projectTypeDictDatas"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="项目状态" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="请选择项目状态" clearable size="small" @change="handleQuery">
          <el-option v-for="dict in statusDictDatas"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['fi:project:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['fi:project:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" fixed="left" prop="tee" type="index" :index="showRn" width="60" />
      <el-table-column label="项目封面" align="center" prop="imgList" >
        <template slot-scope="scope">
          <img v-if="scope.row.imgList" style="width:80px; height:80px;" :src="scope.row.imgList">
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" >
        <template slot-scope="scope">
          <router-link :to="'/fi/project/detailTab/' + scope.row.id" class="link-type">
            <span>{{ scope.row.projectName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="甲方" align="center" prop="thirdName" />
      <el-table-column label="项目地址" align="center" prop="address" />
      <el-table-column label="项目类型" align="center" prop="isNormal">
        <template slot-scope="scope">
          <span>{{ getDictDataLabel(DICT_TYPE.FI_PROJECT_TYPE, scope.row.isNormal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="isFinish">
        <template slot-scope="scope">
          <span>{{ getDictDataLabel(DICT_TYPE.FI_PROJECT_STATUS, scope.row.isFinish) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="区域经理" align="center" prop="areaManagerName" />
      <el-table-column label="项目经理" align="center" prop="projectLeaderName" />
      <el-table-column label="项目主管" align="center" prop="directorName" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['fi:project:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['fi:project:delete']">删除</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit"
                     v-hasPermi="['fi:project:delete']"><router-link :to="'/fi/project/detailTab/' + scope.row.id" class="link-type">详情</router-link></el-button>
          
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="甲方" prop="thirdName">
          <el-input v-model="form.thirdName" placeholder="请输入甲方" />
        </el-form-item>
        <el-form-item label="项目描述" prop="projectDesc">
          <el-input v-model="form.projectDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="项目地址" prop="address">
          <el-input v-model="form.address" placeholder="请选择地址" />
        </el-form-item>
        <amap style="height:240px;" :getAMapObj="getAMapParams"/>
        <el-form-item label="经纬度" prop="latLongItude" style="margin-top:15px;">
          <el-input v-model="form.latLongItude" placeholder="请输入经纬度" />
        </el-form-item>
        <el-form-item label="项目类型" prop="isNormal">
          <el-select v-model="form.isNormal" placeholder="请选择项目类型">
            <el-option v-for="dict in projectTypeDictDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目状态" prop="isFinish">
        <el-select v-model="form.isFinish" placeholder="请选择项目状态">
          <el-option v-for="dict in statusDictDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="区域经理" prop="areaManagerUid">
        <user-select ref="UserSelect" :pModel.sync="form.areaManagerUid" placeholder="请选择区域经理" roleId="10" />
      </el-form-item>
      <el-form-item label="项目经理" prop="projectLeaderUid">
        <user-select ref="UserSelect" :pModel.sync="form.projectLeaderUid" placeholder="请选择项目经理" roleId="11" />
      </el-form-item>
      <el-form-item label="项目主管" prop="directorUid">
        <user-select ref="UserSelect" :pModel.sync="form.directorUid" placeholder="请选择项目主管" roleId="12" />
      </el-form-item>
        <el-form-item label="领班" prop="captainUid">
          <user-select ref="UserSelect" :pModel.sync="form.captainUid" placeholder="请选择领班" :selectChange="multipleSelect" :multiple="true" roleId="13" />
        </el-form-item>
        <el-form-item label="项目主图">
          <image-upload ref="ImageUpload" v-model="form.imgList" :handleSuccess="uploadSuccess"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update, deleteProject, get, getPage, exportExcel } from "@/api/fi/project";
import ImageUpload from '@/components/UploadImage';
import UserSelect from '@/components/UserSelect';
import { getDictDataLabel, getDictDatas, DICT_TYPE } from '@/utils/dict';
import amap from '@/amap'

export default {
  name: "Project",
  components: {
    ImageUpload,    UserSelect,
    amap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        projectName: null,
        projectDesc: null,
        areaManagerUid: null,
        projectLeaderUid: null,
        thirdName: null,
        directorUid: null,
        isNormal: null,
        isFinish: null,
        captainUid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [{ required: true, message: "项目名称不能为空", trigger: "blur" }],
        areaManagerUid: [{ required: true, message: "区域经理不能为空", trigger: "change" }],
        projectLeaderUid: [{ required: true, message: "项目经理不能为空", trigger: "change" }],
        directorUid: [{ required: true, message: "项目主管不能为空", trigger: "change" }],
        isNormal: [{ required: true, message: "项目类型不能为空", trigger: "change" }]
      },
      // 数据字典
      projectTypeDictDatas: getDictDatas(DICT_TYPE.FI_PROJECT_TYPE),
      statusDictDatas: getDictDatas(DICT_TYPE.FI_PROJECT_STATUS)
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      // 执行查询
      getPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    showRn(index){
      return index+this.queryParams.pageSize*(this.queryParams.pageNo -1) + 1
    },
    uploadSuccess(imgUrl){
      this.form.imgList = imgUrl;
    },
    getAMapParams(amap){
      this.form.address = amap.address;
      this.form.latLongItude = amap.center.join(",");
    },
    multipleSelect(val){
      this.form.captainUid = val.join(",");
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        projectName: undefined,
        projectDesc: undefined,
        address: undefined,
        latLongItude: undefined,
        areaManagerUid: undefined,
        projectLeaderUid: undefined,
        imgList: undefined,
        thirdName: undefined,
        directorUid: undefined,
        isNormal: undefined,
        captainUid: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        this.form.imgList = this.form.imgList.join(",")
        // 修改的提交
        if (this.form.id != null) {
          update(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        create(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除项目管理编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteProject(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      // 执行导出
      this.$confirm('是否确认导出所有项目管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportExcel(params);
        }).then(response => {
          this.downloadExcel(response, '项目管理.xls');
        })
    }
  }
};
</script>
